package com.duoec.dw.lineage.service.analyser.statement;

import com.duoec.dw.lineage.dto.statement.DropSqoopStatement;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xuwenzhen
 */
@Service
public class DropSqoopStatementAnalyser extends BaseSqoopStatementAnalyser<DropSqoopStatement> {
    private static final String PREFIX = "alter";
    public static final String FILE_ROOT_PREFIX = "/";

    /**
     * 检查当前行是否需要当前分析器处理
     *
     * @param line 当前行
     * @return true，则会使用analyse方法解析
     */
    @Override
    public boolean check(String line) {
        return line.startsWith(PREFIX) || line.startsWith(FILE_ROOT_PREFIX);
    }

    /**
     * 解析脚本，并返回当前的语句
     *
     * @param lines 所有行
     * @param index 开始行，0开始
     * @return 当前语句
     */
    @Override
    public DropSqoopStatement analyse(List<String> lines, int index) {
        DropSqoopStatement statement = new DropSqoopStatement();
        statement.setStart(index);
        statement.setEnd(index);
        String line = lines.get(index);
        if (line.startsWith(FILE_ROOT_PREFIX)) {
            statement.setPath(line);
        } else {
            statement.setSql(line);
        }
        return statement;
    }
}
